<?xml version="1.0"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:html="http://www.w3.org/1999/xhtml">
  <atom:id>http://bill.welliver.org/atom/pike/Java Bridge</atom:id>
  <atom:title type="text">electronic.alchemy :: Java Bridge</atom:title>
  <atom:updated>2026-07-21T12:27:37-04:00</atom:updated>
  <atom:link href="http://bill.welliver.org/atom/pike/Java Bridge" type="application/atom+xml"></atom:link>
  <atom:link href="http://bill.welliver.org/space/pike/Java Bridge" type="text/html"></atom:link>
  <atom:link href="http://bill.welliver.org/rss/pike/Java Bridge" type="application/rss+xml"></atom:link>
  <atom:generator uri="http://modules.gotpike.org/blahblah/Public.Syndication.ATOM" version="0.1">Public.Syndication.ATOM (Pike v8.0 release 1956)</atom:generator>
  <atom:icon>http://bill.welliver.org/favicon.ico</atom:icon>
  <atom:logo>http://bill.welliver.org/static/images/alchemy.gif</atom:logo>
  <atom:subtitle type="xhtml"><html:div xmlns:html="http://www.w3.org/1999/xhtml">This is a very slightly prettied up version of the presentation I gave at the 2003 Pike Conference in Paderborn. It's basically an overview of what's going on in the Java module. Contact me if you're interested in having a copy of the original text document (it might be easier to read in certain situations).<html:p class="paragraph"/>
<html:b class="bold">JNI Specification</html:b><html:p class="paragraph"/>
Defines methods for embedding a Java VM within an application.
Access to Java objects from the application embedding the Java VM.
Provides access to functions within the application to the embedded Java VM (native methods)<html:p class="paragraph"/>
<html:span class="nobr"><html:img height="9" width="8" src="/static/images/Icon-Extlink.png" alt="[external]"/><html:a href="http://java.sun.com/products/jdk/1.2/docs/guide/jni/">http://java.sun.com/products/jdk/1.2/docs/guide/jni/</html:a></html:span><html:p class="paragraph"/>
<html:b class="bold">The Pike Implementation</html:b><html:p class="paragraph"/>
Located in the Java module.
Supported on systems containing a Java v1.2+ installation.
Known to work with both the Sun JDK and IBM JDK<html:p class="paragraph"/>
<html:b class="bold">Using the Java bridge.</html:b><html:p class="paragraph"/>
<html:ul class="minus">
<html:li>The Hard Way</html:li>
</html:ul>  Java.machine
     starts up a Java VM and leaves the rest to you<html:p class="paragraph"/>
     Pros: Less overhead (speed and memory-wise), more direct control of your 
       interaction with the Java VM
     Cons: Tedious, time-consuming to set up, not very Pikeish<html:p class="paragraph"/>
<html:ul class="minus">
<html:li>The Easy Way</html:li>
</html:ul>  Java.pkg
     will load a java package and connect all of the functions and fields so 
     that they (nominally) act like other pike objects.<html:p class="paragraph"/>
     Pros: much easier to access Java functionality
     Cons: slower, due to object setup and wrappings, less control over how 
       things are set up and connected, doesn't completely hide you from 
       Javaness<html:p class="paragraph"/>
<html:b class="bold">Java VM</html:b><html:p class="paragraph"/>
create(string|void arg)
  In theory, you should be able to start a vm with arg as the classpath. 
  In reality, this doesn't work so well.<html:p class="paragraph"/>
int get_version()
  Returns the version code of the JVM running.<html:p class="paragraph"/>
object find_class(string c)
  Finds the class c, using "/" instead of "." as the class separator.
  Returns the class or zero if the class was not found, in addition to 
  throwing an exception.<html:p class="paragraph"/>
object define_class(object cl, string bc)
  prepare a java class object from a string of java bytecode
  cl is a class loader object
  Note: doesn't seem to work properly.<html:p class="paragraph"/>
int exception_check()
  checks to see if an exception is being thrown.<html:p class="paragraph"/>
object exception_occurred()
  returns the exception object, if one has been thrown.<html:p class="paragraph"/>
void exception_describe()
  prints an exception description and backtrace to stderr<html:p class="paragraph"/>
void exception_clear()
  clears any exceptions<html:p class="paragraph"/>
void  fatal(string arg)
  Raises a fatal error; the VM quits, taking Pike with it.<html:p class="paragraph"/>
object new_boolean_array(int)<html:p class="paragraph"/>
object new_byte_array(int)
object new_char_array(int)
object new_short_array(int)
object new_int_array(int)
object new_long_array(int)
object new_float_array(int)
object new_double_array(int)<html:p class="paragraph"/>
<html:b class="bold">Java Object:</html:b><html:p class="paragraph"/>
mixed cast(string)
  casts the object to another datatype. Currently, only casting to string is 
  supported.<html:p class="paragraph"/>
int `==(mixed)
  are two objects the same?<html:p class="paragraph"/>
int _ _hash()
  calls the hashCode method of the object<html:p class="paragraph"/>
int is_instance_of(object arg)
  determines whether the object is an instance of arg<html:p class="paragraph"/>
object monitor_enter()
  returns a Java.monitor object<html:p class="paragraph"/>
object get_object_class()
  returns the class of an object<html:p class="paragraph"/>
<html:b class="bold">Java Class:</html:b>
inherits Java Object<html:p class="paragraph"/>
object register_natives(array(array(string|function)) arg)
  registers one or more native methods for a class. the class must have the 
  functions defined as "native"<html:p class="paragraph"/>
  arg[n][0] is a string containing the method name to define
  arg[n][1] is a string describing the method signature to define for the given 
  method
  arg[n][2] is a function to be called when the method is called from Java<html:p class="paragraph"/>
object|0 super_class()
  Returns the superclass of the object, zero if the object is a 
  java.lang.Object.<html:p class="paragraph"/>
int is_assignable_from(object c)
 Returns TRUE if:
  The argument refers to the same Java class as the object itself.
  The argument class is a subclass of the object.
  The argument has the object as one of its interfaces.<html:p class="paragraph"/>
void throw_new(string arg)
 Throws an exception or error (if the object is a child one of these classes), 
 with the optional description of arg
 Produces an exception that may be caught or checked using one of the exception 
 checks.<html:p class="paragraph"/>
object alloc()
  allocates a new Java object without invoking its constructors. returns the 
  new object.<html:p class="paragraph"/>
object new_array(int, object|void)<html:p class="paragraph"/>

object get_method(string name, string sig)
  gets method name with signature sig from the class. returns the method or 
  zero if the name and signature do not match any in the class.<html:p class="paragraph"/>
object get_static_method(string, string)
  gets a static method name with signature sig from the class. returns the 
  method or zero if the name and signature do not match any in the class.<html:p class="paragraph"/>
object get_field(string name, string sig)
  gets a field name with a type of sig from the class. returns the
  field object or zero if the name and signature do not match any in the class.<html:p class="paragraph"/>
object get_static_field(string, string)
  gets a static field name with a type of sig from the class. returns the
  field object or zero if the name and signature do not match any in the class.<html:p class="paragraph"/>

<html:b class="bold">Java Throwable</html:b>
inherits Java Object<html:p class="paragraph"/>
void throw()<html:p class="paragraph"/>
<html:b class="bold">Java Array</html:b>
inherits Java Object
_sizeof()
`[]
`[]=
_indices
_values<html:p class="paragraph"/>
<html:b class="bold">Java Static Method</html:b><html:p class="paragraph"/>
mixed `(mixed&#x2026; args)
  calls the method with arguments args.<html:p class="paragraph"/>
<html:b class="bold">Java Method</html:b><html:p class="paragraph"/>
mixed `(object obj, mixed&#x2026; args)
  calls the method as defined in object obj, with arguments args.<html:p class="paragraph"/>
mixed call_nonvirtual(object obj, mixed &#x2026; args)
  calls the method as defined in the class the method is derived from, with 
  arguments args.<html:p class="paragraph"/>
<html:b class="bold">Java Field &amp; Static Fields</html:b><html:p class="paragraph"/>
mixed set(mixed val)
  sets the value of a field to val.<html:p class="paragraph"/>
mixed get()
  returns the value of a field<html:p class="paragraph"/>
Objects used internally by the module:<html:p class="paragraph"/>
  Java Monitor
  Java Natives
  Java Attachment<html:p class="paragraph"/>
  
<html:b class="bold">The Java Package interface</html:b><html:p class="paragraph"/>
The Java.package module provides convenient access to an entire Java Class 
package, performing a number of useful tricks along the way.<html:p class="paragraph"/>
The module will look for a package, load the class, set up access to all of 
its fields and methods, making it look very much like a Pike object.<html:p class="paragraph"/>
Usage: 
<html:div class="code"><html:pre><html:pre>
        &gt; <html:b><html:font color="darkgreen">object </html:font></html:b><html:b><html:font color="darkbrown">i=Java.pkg&amp;#91</html:font></html:b>;<html:i><html:font color="darkred">"java/lang/Integer"</html:font></html:i>];
        &gt; i(52);
        &gt; indices(i);
        Result: ({ <html:font color="red">/* 14 elements */</html:font>
                <html:i><html:font color="darkred">"wait"</html:font></html:i>,
                <html:i><html:font color="darkred">"getClass"</html:font></html:i>,
                <html:i><html:font color="darkred">"doubleValue"</html:font></html:i>,
                <html:i><html:font color="darkred">"longValue"</html:font></html:i>,
                <html:i><html:font color="darkred">"toString"</html:font></html:i>,
                <html:i><html:font color="darkred">"equals"</html:font></html:i>,
                <html:i><html:font color="darkred">"compareTo"</html:font></html:i>,
                <html:i><html:font color="darkred">"hashCode"</html:font></html:i>,
                <html:i><html:font color="darkred">"floatValue"</html:font></html:i>,
                <html:i><html:font color="darkred">"shortValue"</html:font></html:i>,
                <html:i><html:font color="darkred">"notifyAll"</html:font></html:i>,
                <html:i><html:font color="darkred">"notify"</html:font></html:i>,
                <html:i><html:font color="darkred">"intValue"</html:font></html:i>,
                <html:i><html:font color="darkred">"byteValue"</html:font></html:i>
            })
          &gt; (<html:b><html:font color="darkgreen">string</html:font></html:b><html:b><html:font color="darkbrown"/></html:b>)i-&gt;toString();
          Result: <html:i><html:font color="darkred">"52"</html:font></html:i>
</html:pre></html:pre></html:div><html:p class="paragraph"/>
          
What the Java.package module does not do:<html:p class="paragraph"/>
<html:ul class="minus">
<html:li>convert Pike datatypes into Java datatype objects</html:li>
<html:li>convert Java datatype objects into Pike datatypes</html:li>
<html:li>attempt to guess what function signature you wish to use if it's at all ambiguous.</html:li>
<html:li>completely mask the Java bridge and its Javaness from the Pike user, though it comes quite a bit of the way.<html:p class="paragraph"/></html:li>
</html:ul><html:b class="bold">Using Java.package</html:b>
  The package interface to Java is straightforward in use. The Java module
  creates an instance of the package interface called "pkg" on startup.<html:p class="paragraph"/>
  The package interface exposes its functionality by overloading the indexing 
  operator.<html:p class="paragraph"/>
  To access a java class, simply ask Java.pkg for it:<html:p class="paragraph"/>
  &gt; object myclass = Java.pkg["org/welliver/conference/myclass"];
  Result: Java.jclass()<html:p class="paragraph"/>
  An error will be thrown if the requested class cannot be found.<html:p class="paragraph"/>
<html:b class="bold">Java.jclass</html:b>
  represents a Java Class in Pike.<html:p class="paragraph"/>
  `(mixed &#x2026; args) calls the constructor that most closely matches args. will
     not attempt to choose if more than one method signature matches.
  `[n] returns the value of a static field n or a static method object n
  `-&gt;n returns the value if `[n] unless n[0..0]=="_", in which case it returns:<html:p class="paragraph"/>
    _fields:  returns a mapping of the field objects in the class
    _static_fields: returns a mapping of the static field objects in the class
    _methods: returns a mapping of the method objects in the class
    _static_methods: returns a mapping of the method objects in the class
    _wrap_result: returns the wrap result function, used to turn a Java.object 
      into a Java.jobject
    _method: returns the method selector function
    _constructor: returns the constructor selector function
    _alloc: returns the alloc function of the base Java.object
    _register_natives: returns the register_natives function of the base 
      Java.object<html:p class="paragraph"/>
    Java.jmethod _method(string n, string sig) 
      returns the java method that matches name n and type signature sig.
    Java.jconstructor _constructor(string sig)
       returns the java constructor for this class that matches type signature 
       sig.<html:p class="paragraph"/>
<html:b class="bold">Java.jconstructor</html:b>
  represents a Java constructor in Pike.<html:p class="paragraph"/>
  Java.jobject `(mixed &#x2026; args)
    calls the constructor of the class with arguments args.<html:p class="paragraph"/>
<html:b class="bold">Java.jmethod</html:b>
  represents a Java method in Pike.<html:p class="paragraph"/>
  Java.jobject `(mixed &#x2026; args)
    calls the function with arguments args.<html:p class="paragraph"/>
  Java.method for_protos(string sig)
     returns the Java method matching the type signature sig. Used internally.<html:p class="paragraph"/>
  Java.jmethod for_object(object obj)
      returns a jmethod object for the object obj. Used internally.<html:p class="paragraph"/>
<html:b class="bold">Java.jobject</html:b>
  represents a Java object in Pike.<html:p class="paragraph"/>
  cast(mixed type)
     calls the cast function for the underlying Java object. Note that this
     only works with the argument of "string".<html:p class="paragraph"/>

  `[n]
    returns the field or method n from the class.
  `-&gt;n
    returns the value of `[n] unless n[0..0]=="_", in which case it returns:<html:p class="paragraph"/>
    _method: returns the method selector function.
    _obj: returns the low level Java.object object.
    _monitor_enter: returns the Java.monitor object associated with this class.<html:p class="paragraph"/>
  _values()
    returns the values of the object, matching its indices.
  _indices()
    returns the indices of the object, ie field and method names.<html:p class="paragraph"/>
  Java.jmethod _method(string n, string sig) 
    returns the java method that matches name n and type signature sig.<html:p class="paragraph"/>
<html:b class="bold">Java.jarray</html:b>
   represents a Java array in Pike.<html:p class="paragraph"/>
   cast(mixed &#x2026; arg)<html:p class="paragraph"/>
   `[n] 
     returns the nth element of the array:<html:p class="paragraph"/>
     length: returns the number of elements in the array
     _monitor_enter: returns the monitor object associated with this instance
     _obj: returns the low level object for this array<html:p class="paragraph"/>
   `[n]=x
     attempts to set the value of the nth element to x.<html:p class="paragraph"/>
   `-&gt;n
     returns the following:<html:p class="paragraph"/>
     length: returns the number of elements in the array
     _monitor_enter: returns the monitor object associated with this instance
     _obj: returns the low level object for this array<html:p class="paragraph"/>
   _sizeof()
   _indices()
   _values()<html:p class="paragraph"/>
     overloads to make an array object seem more Pikeish.<html:p class="paragraph"/>
<html:b class="bold">Java Convenience Functions</html:b><html:p class="paragraph"/>
The following functions will convert a Pike datatype to a relatively common 
Java equivalent. Their behavior is general, and any value may be provided to 
them, as long as they are the specified type. Any exceptions are noted below.<html:p class="paragraph"/>
Java.JInteger(int i)
Java.JString(string s)
Java.JFloat(float f)
Java.JBoolean(int b)
  any value other than 0 is true, 0 is false.
Java.JArray(array(mixed) a)
  can encode an array of any type, however all of the elements must be the 
  same type. that is, you may not mix strings and floats as values in the array
Java.JHashMap(mapping m)
Java.JVector(multiset x)
</html:div></atom:subtitle>
</atom:feed>
